package com.base.commonlib.base;

import android.app.Activity;
import android.content.Context;
import com.base.commonlib.utils.RomUtils;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Build;
import android.os.Bundle;
import me.yokeyword.fragmentation.SupportActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.http.utils.ScreenUtil;
import com.base.commonlib.http.utils.SnackBarUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.commonlib.widget.CommonDialog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


public abstract class BaseActivity extends SupportActivity {

    protected Context mContext;
    protected ViewDataBinding mRootBinding;
    protected CommonDialog mWaringDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setOnOrientation();
        ScreenUtil.adaptScreen4VerticalSlide(this, 360);
        BaseApp.addActivity(this);
        ARouter.getInstance().inject(this);
        setLightStatusBar(this, false);
        mContext = this;
        try {
            mRootBinding = DataBindingUtil.setContentView(this, getLayoutResource());
        }catch (Exception e)
        {

        }
        mWaringDialog = new CommonDialog(mContext, "确定要退出？");
        mWaringDialog.setOnPosClick(new CommonDialog.OnPosClick() {
            @Override
            public void onClick() {
                finish();
            }
        });


    }

    public void setOnOrientation(){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }


    void showMesWActionB(String msg, String action, View.OnClickListener listener) {
        SnackBarUtil.showMsg_Action(mRootBinding.getRoot(), msg, action, listener);
    }


    void showMesB(String msg) {
        Toast toast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
//        SnackBarUtil.showMsg(mRootBinding.getRoot(), msg);
    }


    protected boolean checkTextView(TextView tv) {


        if (TextUtils.isEmpty(tv.getText().toString().trim())) {
            return false;
        } else {
            return true;
        }
    }

    protected boolean checkEditText(EditText et) {
        if (TextUtils.isEmpty(et.getText().toString().trim())) {
            return false;
        } else {
            return true;
        }
    }


    protected boolean checkSpinner(Spinner sp) {
        if (sp.getSelectedItem() != null) {
            return true;
        } else {
            return false;
        }
    }


    protected void customStartActivity(Intent intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    protected void customStartActivityForResult(Intent intent, int requestCode) {
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseApp.removeActivity(this);
        if (mRootBinding!=null)
        mRootBinding.unbind();
    }

    protected void startLogin(){
        Intent in = new Intent();
        try {
            in.setClass(mContext, Class.forName("com.base.mine.PwdLoginActivity"));
            startActivity(in);
            finish();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            ToastUtil.show("跳转登录失败，请联系管理员");
            finish();
        }

    }
    /**
     *  修改状态栏文字颜色，这里小米，魅族区别对待。
     */
    public static void setLightStatusBar(final Activity activity, final boolean dark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            switch (RomUtils.getLightStatusBarAvailableRomType()) {
                case RomUtils.AvailableRomType.MIUI:
                    MIUISetStatusBarLightMode(activity, dark);
                    break;

                case RomUtils.AvailableRomType.FLYME:
                    setFlymeLightStatusBar(activity, dark);
                    break;

                case RomUtils.AvailableRomType.ANDROID_NATIVE:
                    setAndroidNativeLightStatusBar(activity, dark);
                    break;

            }
        }
    }

    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }
    public static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
        boolean result = false;
        Window window = activity.getWindow();
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && RomUtils.isMiUIV7OrAbove()) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    if (dark) {
                        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    } else {
                        activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                    }
                }
            } catch (Exception e) {

            }
        }
        return result;
    }

    private static boolean setFlymeLightStatusBar(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

    protected abstract int getLayoutResource();
    protected abstract void init();
    protected abstract void start();
}
